home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-05-15 | 53.8 KB | 1,057 lines |
- Chapter1 page 1 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
-
- CHAPTER 1
-
-
- 1.0 Making Friends With Your Computer
-
- For some reason, when most people first meet computers they
- are afraid of them. If they sit down at the keyboard they tend
- to freeze up. Visions of the computer running amok and causing
- tremendous amounts of destruction appear in their minds. Well
- just take a look at one. Does it look fierce? It may look
- complicated but it certainly doesn't look terrifying. Young
- children given the chance will spend hours pecking at the
- keyboard, so why should such a device inspire fear or feelings of
- inadequacy in adults. There is absolutely no reason to be afraid
- of it. First of all, it is completely benign. It has no
- feelings at all. It is an inanimate object. You can't hurt its
- feelings even if you physically assault it with a hammer, blow
- torch or other instrument of destruction. You may destroy it,
- but you definitely won't hurt its feelings.
-
- The computer can be likened to an unfinished being. It has
- been created (built) and is complete. It now requires
- instructions to perform any task. These instructions are called
- computer programs or software, and the act of putting together
- these instructions is called programming.
-
- Once a program is put into the computer it "comes to life".
- It can now do something. That something is almost anything that
- the human mind can conjure up. The computer can play a game, can
- control what ever is plugged into it, can teach, can in fact do
- what ever its instructions tell it to do.
-
- The first thing to remember is that the computer always does
- what it is told to do. That means for example if you are doing
- the programming, that the computer does what you tell it to do,
- which is not necessarily the same thing as what you want it to
- do. If you make a mistake in instructing the computer, how is
- it supposed to know what you meant to tell it?
-
- The best way to learn how to tell it to do things (ea.
- learning to program it) is to work with it. Thus first read
- the books that come with the machine that tell you what it does
- and how to use it. Then try and read a book that someone else
- has written about the machine that explains how to use it in
- greater detail than the documentation supplied by the
- manufacturer. You may then be ready to have a go at it yourself.
-
- Once you have decided to buy a computer make friends with
- it. In fact even if you haven't yet decided to buy one, its not
- a bad idea to make friends with one. Go to a computer store or
- the home of a friend who has one, sit down in front of the thing
- and play a few games. Games are the best method of introducing
- the beginner to the computer.
-
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 2 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- First familiarize yourself with using the computer, by
- playing with it. You can do an awful lot with a computer without
- knowing the first thing about programming. Applications packages
- abound. Apart from the myriads of games there are many serious
- packages such as spread sheet calculators, word processors,
- custom packages for various professions such as doctors or
- dentists, and financial and other business programs. Each comes
- with its own documentation that explains how to use the software.
- You only need to get involved with programming if you intend to
- write your own software or customize existing software to your
- requirements.
-
- When getting involved with programming start by typing up a
- program from a book or a magazine. If you don't understand the
- programming language, don't try to follow what is happening, just
- concentrate on not making any mistakes. You will become familiar
- with the jargon by working with it. The terms will enter your
- sub-conscious and you will slowly learn what they mean. When you
- type in a program, save what you have entered every few lines.
- If you are working with cassettes play back the cassette from
- time to time to verify that the recorder is adjusted properly.
- There are few things more frustrating than spending hours working
- at the machine only to lose the result due to a power failure or
- bad disk/cassette save operation. Once you have entered the
- complete program into the machine, save it, then have someone
- else help you verify the typing by reading the listing as you
- check the screen copy. It is best to work from the end of the
- listing back to the beginning if you can, so that you will not
- tend to follow the "story" and anticipate what is coming next.
- Edit any mistakes out of the program, save the verified version
- and then run the program. The first time around several things
- may happen including
-
- 1) the program performs as the author said it would,
-
- 2) the program does weird and wonderful things approximating to
- what the author promised it would,
-
- 3) the computer displays error messages,
-
- 4) a combination of 2) and 3) occurs.
-
- In the event that the computer fails to perform as the
- author said it would, recheck the program against the original
- magazine or book listing. If the program is written in BASIC and
- you get an error message of the type SYNTAX ERROR AT LINE .....
- the computer is telling you that you have made a typing error in
- line .... . If the program has somehow been corrupted, reload
- it, reverify the listing and try again. If you still can't find
- anything wrong then in the case of a magazine article you can
- either hopefully wait for a correction to be published a few
- months later or try and find the error yourself. For that "User
- groups" and computer clubs are very handy, because they include
- people who have the same computer that you do and may know a
- little more about it than you. In fact it is advisable to try to
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 3 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- talk about the program before you even begin to type it up for
- you may find someone in the User group or club who has already
- typed it up and gotten it working. The procedure of getting the
- program working by taking out the errors (known in the trade as
- "bugs") is called de-bugging the program. You will then be
- forewarned as what to do to get the program to work, or better
- still be supplied with a cassette or disk for your personal use,
- saving you the bother of typing the thing up yourself.
-
- Once the program is working correctly (more or less), you
- are ready to start modifying the program. Get hold of a book
- that describes the programming language that you are working with
- so that you can use it as a reference book and dictionary.
- Dissect the program to see what the author made the computer do.
- See how the idea for the game was translated into a computer
- program. Trace the flow of the program and make small changes.
- For example, change the color of the display; change the dialog;
- as you perform these small modifications you will build up
- confidence in yourself. You are now instructing the machine.
- You are taking over from the original programmer and are making
- it perform to your wishes. You are becoming a programmer. When
- you have played with other people's programs for long enough the
- time will come when you are ready to write your own program.
-
- All computer applications are limited by either hardware or
- software. Control applications may be limited by the input/output
- (i/o) capabilities of the machine, games may be limited by the
- amount of memory in it, graphic capabilities are limited by both
- the resolution of the screen, the colors available and the speed
- that the computer can update the screen.
-
- The first generation of computer games were limited in this
- manner. In the early days of personal computers, the only
- terminal available was a teletypewriter. This was a slow machine
- which printed capital letters on paper. This type of machine is
- still used in TELEX applications. The computing power was there
- but the graphics i/o capability wasn't. The first games were
- thus mostly some sort of simulation games with a minimal amount
- of i/o. Video displays and CRT terminals soon followed. The
- same technology that brought us the video displays also allowed
- for video games. At first these games were dumb, namely tennis
- or ping-pong. then as the technology improved, games and personal
- computers also improved.
-
- The following generation of computers were designed mainly
- as game playing machines and incorporated relatively high
- resolution color graphics capability. The lowest resolution
- machines were those designed to work into the antenna of a
- conventional television receiver. These machines made real time
- action games available in the home and have proved to be very
- popular. Higher resolution plug in add on's and video monitors
- added more and more capacity to the machines and as technology
- progresses games became and will become more complex and
- sophisticated, the i/o interface will become better, more "on-
- line" memory will become available and greater realism will
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 4 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- become possible.
-
- The simulation games were superseded by the action games
- which required some degree of co-ordination yet little in the way
- of thought. In the current generation of simulation games, the
- game is programmed about a behavior model. In this book we shall
- discuss a Simulation, the theory behind it and the techniques
- that can be used to translate the simulation model into a
- workable and enjoyable game.
-
- The computer is dumb. It does exactly what you tell it to
- do. Not only that, you have to tell it in detail how to do what
- you want it to do. There is no point in telling the computer to
- "rule the world", you have to tell it in detail how to build up
- armies, cause subversion in other countries, fight battles and
- then set up your world government. You may ask what is the
- point of having a computer, if it has to be told everything in
- detail. If you are going to have to explain each point
- meticulously, you may as well do the job yourself. Well the
- computer is best doing repetitive tasks. If the calculation is
- to be done once, use a pocket calculator, if it is going to be
- done many times, use a personal computer.
-
- The computer has to be instructed by a human being. When
- there is a mistake in a bill or account statement, it is very
- unlikely that the computer is at fault no matter what you are
- told. The fault is most probably human. The mistake is either in
- the program or more probably the mistake was made at the time of
- the entry of data into the computer. If the person entering the
- amount of your payment into the computer makes a mistake by
- entering the wrong amount and the mistake is not caught, the
- program will grind on on its way and will send you messages based
- on the faulty input data, because after all, how was it supposed
- to know. Amongst computer professionals this phenomenon is
- known as "garbage - in, garbage - out" (GIGO).
-
- Computer languages are the means by which people instruct
- computers to perform tasks. Any person who wishes to control the
- operation of a computer rather than just to communicate with it
- thus has to learn a programming language. Learning a computer
- language is easier than learning a foreign language; such as
- French, Spanish, Japanese or Hebrew because in a computer
- language a word or expression has only one meaning, and the
- computer does exactly what it is told to do.
-
- Learning a language is a chore unless you remember that it
- is really a means to an end. The end is the task that you want
- the computer to do, the means is the language that you use to
- tell the computer how to do the task. If you can define the task
- first, then use the language to make the computer perform that
- task, and you are really interested in that task, learning the
- language will become a by-product of the operation.
-
- Talking to the computer in a language that it understands is
- called programming. Programming is the art of writing programs
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 5 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- or sequences of instructions for the computer to perform. Being
- able to write programs is not the same as being able to write
- good programs. There are many people who earn their living
- writing computer programs. There are other people who earn their
- living fixing up programs written by other so called
- "programmers". Computer programming is easy. Any idiot can do
- it. Good computer programming is difficult but can be learnt.
- If you are learning your first computer language, you are also
- learning programming, which is not the same thing. There are
- many programmers who know FORTRAN or BASIC yet do not know a word
- of FORTH. There are people who know many languages yet are
- terrible programmers, so if this is your introduction to BASIC,
- you are also learning programming as well as BASIC and it is as
- well to differentiate between the two. The habits you form now
- will govern your programming ability from now on, so be sure that
- they are the correct ones.
-
- Computer programs operate on three categories of data. They
- are constants, variables and parameters. The data elements may
- be simple or complex and stored as single items or as rows or
- arrays of elements.
-
- A constant is a fixed value that never changes, such as 1,
- 2, 88, 1023 etc.
-
- A variable is a data element that changes its value while
- the program runs. It is usually referenced by a name such as
- APPLE or A0. The address of the variable is the location that it
- is stored in memory, the contents of the location are the value
- assigned to the variable.
-
- A parameter has a value that is fixed for a particular run
- of the program. For example, in space war games, the number of
- enemy vessels at the start of each game may be different but
- constant for the particular game. That number can thus be
- considered as a parameter.
-
- Since most BASIC interpreters process constants differently
- to the way they operate on variables, it is usually advisable to
- store constants as parameters. Thus for example, instead of using
- 0.1 as a constant if it will be used many times, use P1 (point
- one) as a parameter equal to 0.1 Values are assigned to
- variables and parameters using the LET statement in BASIC such as
- in the statement
- LET P1 = 0.1
- Alternatively, you will also meet the LET statement in the
- format
- LET A0 = A0 + 5
- Here, the statement means, take the current value stored in
- the variable A0, add 5 to it and save the result (in the location
- addressed as) A0. If you are at all familiar with algebra, you
- will have noticed right away that there was something wrong with
- the equation. You are correct, the statement is not an equation,
- it is an expression. It could have been expressed as
- LET A0 + 5 -> A0.
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 6 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
-
- Data comes in a number of formats. If the format is numeric
- (ea. it is numbers), the data can be integer or floating point.
- Integer numbers do not have a decimal part. They are whole
- numbers, floating point numbers have decimal parts. Example of
- floating point numbers are 1.652, 12.13, 0.003, and 567.8. Note
- the different position of the decimal point in each of the
- numbers, namely it floats around.
-
- An other form of data is text material in ASCII format.
- These are words that make up headings and titles, such as "SHORT
- RANGE SENSORS" or "PHASERS". In BASIC such material are called
- "strings" because they consist of strings of letters joined
- together. Variables, constants or parameters containing strings
- of text are called string variables, string constants or string
- parameters. They are identified by a dollar sign termination
- (pronounced "string") such as A$ ("A string") or D$ ("D string"),
- and can be treated just like regular numeric variables, thus
-
- LET A$ = "SHORT RANGE SENSORS" sets up the contents of the string
- variable A$ as SORT RANGE SENSORS. Quotation marks have to be
- used in BASIC to define the start and the end of the string of
- characters.
-
- Variables can be stored individually or in arrays.
- Individual variables can be named, such as APPLE, PEAR, ORANGE or
- BANANA. An array can be considered as a group of variables that
- can be referenced with respect to some kind of common
- denominator. For example, if we had an array named FRUITBOWL
- then each item in the array can be a separate fruit, and can be
- referenced with respect to the FRUITBOWL as FRUITBOWL(1) for the
- first element, FRUITBOWL(9) for the ninth, etc.
-
- An ideal task for the beginner to learn a language on is a
- simulation game which is written around the computer that the
- beginner has available. For in that case, there is complete
- control of all inputs and outputs, This kind of game in which
- the player makes decisions based on the information available to
- him or her available at the time), can be made sufficiently
- sophisticated and complex so as to make writing it an adequate
- challenge for anybody.
-
- The techniques used in writing a good game are the same that
- programmers use in professional activities. Writing a good game
- poses a challenge that allows you to develop good habits and
- techniques for programming and also allows you to learn a
- language in an interesting manner. By taking an orderly approach
- to the game design, complex operations may be clearly understood
- and converted to computer code with the aid of a language
- reference manual, irrespective of the language being used.
-
-
- 1.1 Defining the Game or Task
-
- Pick a task that interests you. you can invent a new game
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 7 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- or rewrite an existing one. If you are familiar with a game
- written in one language such as BASIC, and are learning another
- such as FORTH, examining the different approaches used by
- programs written in two languages to achieve the same end results
- will be educational in itself.
-
- If you plan to interest others in playing your game once you
- have finished it, there are a number of factors to be considered.
- The first one is the ease of learning how to play the game.
- There should be no need for someone to sit down and study a 176
- page manual (no matter how well written) in order to learn the
- rules. If you can model real life, a well known television
- series, a best selling book or some other widely known
- environment, your game will have a wider appeal than if you had
- invented the game from scratch. The disadvantage of basing the
- game on an existing work of fiction is that it has been
- copyrighted and depending on what you plan to do with your
- finished product, you may have to enter into negotiations with
- the owner of the copyright. If you invent your own game, or your
- own situations, try and use characters or situations that are
- familiar to the players before they sit down to play the game.
- If you cannot do this, then make the options available to the
- player self evident.
-
- The second factor involved is the degree of complexity or
- sophistication of the game. The more complex it is, the greater
- the enjoyment in playing it, however that also means that it will
- be more difficult to learn the rules. There is a tradeoff
- involved between "getting them interested" and "keeping them
- interested".
-
- Another factor is realism. the player has to believe that
- the game takes place in a logical and believable environment.
- Don't create an absurd or unbelievable situation and expect
- anyone to play your game more than once. Books, films and plays
- are entertainment. A computerized game is interactive
- entertainment. You, the game writer create the background and
- set up the rules for the consequences of any action. you then
- let the player wander through your creation, and each playing of
- the game will be different. It is this difference that poses the
- challenge and makes them come back again and again.
-
- Embellishments or "bells and whistles" can leave a very
- positive impression on your game players. How many times have
- you heard said about a movie that "the plot was terrible, but the
- special effects were great". Put as many special effects as you
- can within reason into your game.
-
- The first step in writing the program is coming up with the
- idea. What should the program do? What do you want the program
- to do? A game is a good first time program because you have
- total control over all aspects of the task. You can decide the
- rules. You can decide on the i/o. You don't have to build or
- buy a special interface for the computer to talk to the outside
- world. Your program can be self contained.
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 8 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
-
- Once you have developed the idea write it down. This
- becomes the specification for the program. It should describe in
- words what the program is to do. It does not necessarily
- describe how it will do the job. The specification is thus
- independent of the programming language.
-
- The next stage is to decide on the language. The decision
- may be made for you, for example if the personal computer comes
- with a built in language when you purchase it, and you don't have
- any others, that is the one you will use. Most computers at
- this time come with a dialect of BASIC built into them. The
- vast majority of computer programs can be written in almost any
- computer language. Some programs however are optimally written
- in languages designed for particular applications. FORTRAN for
- example optimizes mathematical computations, COBOL was designed
- for business applications, BASIC for teaching programming, FORTH
- for process control, and so on.
-
- To repeat, a game is an ideal task for learning a computer
- language. In defining the game you set up all the conditions and
- constraints, you are bound only by your imagination and the
- physical limitations of the computer. (There is no point in
- writing a graphic shooting gallery game if the only operating
- interface you have is a teletypewriter).
-
- Once the task is defined, you know what the computer is
- going to do. The next stage is to decide how it is going to do
- it. The complete task must be broken down into a number of
- sequential steps. How detailed these steps have to be is a
- function of what level of language is being used to write the
- program. For example, adding two variables together could be
- written in BASIC as
- LET A0 = A1 + A2
- which instructs the computer to add together the values assigned
- to the variables A1 and A2 then to store the result in the
- variable A0. A similar sequence written in 8080 assembly
- language could look something like
-
- LDA A1 ;LOAD THE CONTENTS OF A1 INTO THE ACCUMULATOR
- LXI H,A2 ;POINT THE H AND L REGISTERS AT A2
- ADD M ;ADD THEM
- STA A0 ;AND SAVE THE RESULT IN MEMORY AT LOCATION A2
-
- In the low level assembly language, you have to assume that the
- computer knows nothing. In order to have it add those two
- values, not only do you have to tell it to add the numbers, but
- you first have to tell it where they are stored in memory, then
- you have to tell it to fetch them from memory, add them together
- and where to store the result. In the higher level language, you
- don't care where the data are stored in memory, you let the
- computer assign memory space to the variables, and all you do is
- tell it to add them.
-
- Earlier we saw that the computer is ignorant until
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 9 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- instructed otherwise. How then can the computer know by itself
- where to store those numbers? A statement such as LET A0 = A1 +
- A2 only tells the computer what to do with A0, A1 and A2, it says
- nothing about where they are located in memory.
-
- In order to understand this we have to take a brief look at
- the internal hardware of the computer as shown in figure 1.1.
- The majority of the hardware consists of memory which is used to
- store both instructions (programs) and data. The instructions
- are carried out in the Arithmetic - Logic Unit (ALU) of the
- Central Processing Unit (CPU). The interface to the user is
- located in the i/o module.
-
- When the computer is first turned on, the internal circuitry
- of the hardware dictates the memory location that will be read as
- the first instruction. Each time that the microcomputer is
- "powered up", that location will be assumed to contain the fist
- instruction. In the case of the 8080 or Z80, that location is
- the first location of memory at an address of 0. The starting
- location for other microprocessors differs. Most microcomputers
- contain hardware read only memory (ROM) at the starting address
- location or some other circuitry to fool the CPU into thinking it
- is looking at the starting address when it is really looking
- elsewhere. The actual task performed by the program in the ROM
- at the starting location differs in each machine. In some, it
- may initialize a monitor program such as AMS-80, in others it may
- initialize BASIC, or boot up a Floppy Disc Operating System
- (DOS).
-
- Now consider what actually happens when when an instruction
- is carried out. At the micro - micro level, just after
- initialization, the computer is going to carry out its first
- instruction. The first thing it does is fetch the first part of
- the instruction from memory into the Central Processing Unit
- (CPU). The hardware in the CPU determines the type of the
- instruction and then performs the remainder of the instruction.
- Each instruction is divided up into a number of parts. There is
- the fetch cycle in which the instruction is moved from memory
- into the CPU and then there is the execution cycle in which the
- instruction is carried out. The mechanism to perform these
- functions are built into the hardware of the machine and are
- known as the micro code. Different microprocessors have different
- micro codes and thus also have different sets of instructions.
- There are some special purpose microprocessors (usually of bit
- slice architecture) that allow you to define your own micro code
- and thus your own basic instruction set.
-
- The micro code operates on a set of bits called a byte. If
- the machine is an 8 bit machine it has an accumulator that is 8
- bits wide, if it is a 16 bit machine it has an accumulator that
- is 16 bits wide. The byte in the accumulator is represented in
- binary (Base 2) math as a number. For our convenience as
- programmers, these binary numbers are grouped together and
- represented by Octal (Base 8) or Hexadecimal (Base 16) values.
- An example of such a number is 11000011 in Binary, 303 in Octal
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 10 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- or C3 in hexadecimal notation. The CPU works with digital
- signals that only have "on" or "off" states, represented by a 0
- for "off" and a 1 for "on". It is much more convenient to refer
- to C3 00 F0 as an instruction than 11000011 00000000 11110000.
-
- If memory is filled with binary numbers, how then does the
- computer know which are instructions and which are data? The
- microcode knows how many bytes an instruction requires. That
- knowledge is built into the hardware as part of the architecture
- of the machine. So once it has read and decoded the first byte
- it knows what is to follow. The first byte is known as the
- operation code (op code) because it defines what operation is to
- be performed. Thus for example in 8080 code, a branch or jump
- instruction that causes the program flow to continue from some
- location other than the next one in the sequence, is always
- followed by two bytes containing the address of that location.
-
- The first few instructions in the start up ROM are in binary
- code. Since binary code even in its Octal or Hexadecimal
- representation is not easy to use for most programmers, assembly
- language and assembler programs were invented. Assembly language
- allows us to refer to an instruction in a pseudo english manner
- somewhat symbolic of what the instruction really does, for
- example, again in 8080 assembly language JMP means jump (to an
- other instruction located somewhere else in memory), MOV means
- move something from one register to an other, MVI means move
- something (like a constant) immediately into a register. The
- assembler also allows us to refer to memory locations by name
- instead of by the address of the absolute location.
-
- Figure 1.2 is an example of a program listing made using an
- assembler. An editor program is first loaded into the computer.
- This is a (rudimentary) word processor that allows the assembly
- language instructions to be stored in machine readable form. The
- assembler is then invoked to convert the Assembly Language Source
- code to a binary object code. This object code is then loaded
- into memory and is then executed by the machine in turn. Note
- the use of comments after the instruction. The format of the
- listing is address location, hexadecimal representation of the
- object code, assembly language (split into op code and numeric
- parts) and comments. In 8080 assembly language, the comments
- have to be preceded by a semi colon. In BASIC comments are
- preceded by a REM statement. Comments are very important and are
- used to explain why something was done in a particular manner.
- They serve two purposes: the first being to explain to others
- what you did and how you did it, the second reason is to remind
- you of what you did when you come to make some modifications to
- the program after some time has elapsed since you wrote it.
-
- Writing in assembly language has many disadvantages
- including the one that the programmer has to give very detailed
- instructions and has to keep track of the locations of all the
- data. There must be a better way, and for many applications
- there is, and it is known as a high level language.
-
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 11 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- 1.2 The high level language
-
- The high level language of which BASIC is just one example
- is itself a program that is a tool which allows the programmer to
- concentrate on telling the computer what to do. In assembly
- language, not only do you have to tell the CPU what to do and how
- to do it, but you also have to tell it where everything is. The
- assembler facilitates the job but the onus is still on the
- programmer. The high level language on the other hand has built
- into it a lot of "where" information. From now on, when using
- the high level language, if you define a variable such as APPLE,
- the language will assign a memory location to APPLE and some
- bytes (space) to store data in that location. Most versions of
- BASIC do not even require you to tell the computer that you are
- going to use APPLE as a variable until the first time that you
- actually use it.
-
- If we go back to the A0 = A1 + A2 example that was mentioned
- above, the full assembly language listing also has to include
-
- A0: DS 1 ;DEFINE SPACE FOR 1 MEMORY LOCATION
- A1: DS 1 ;DEFINE SPACE FOR 1 MEMORY LOCATION
- A2: DS 1 ;DEFINE SPACE FOR 1 MEMORY LOCATION
-
- which tells the assembler to define the locations of the
- variables and reserve one byte of memory to be associated with
- each of them. If these pseudo instructions were not included in
- the source code listing, the assembler would flag every reference
- to A0, A1 or A2 as an error (referencing an undefined memory
- location). The use of the term pseudo instruction, defines a
- category of directives to the assembler, rather than instructions
- that are executed by the CPU.
-
- Consider now what happens when BASIC meets and processes the
- instruction LET A0 = A1 + A2 which instructs the computer to
- add together the values assigned to the variables A1 and A2 then
- to store the result in the variable A0. BASIC first locates the
- variable A1 and A2 and adds their contents together. It then
- searches for and stores the result in the variable A0. If A0 has
- not yet been defined, BASIC allocates space for it at this time.
- The programmer does not know where the variables are stored in
- memory and in most instances does not care.
-
- BASIC is available as an interpreter or as a compiler. The
- interpreter resides in memory together with the program. It
- sequentially interprets each line of program in turn, converting
- it on the fly to binary object code and executing it. Since each
- line of code is interpreted each time it is executed the running
- speed of the program is relatively slow. A compiler for BASIC on
- the other hand performs an operation on the program written in
- BASIC analogous to the operation of an assembler on assembly
- language to convert it to an object code module. Since the
- computer then executes a compiled binary object module the
- execution time is speeded up.
-
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 12 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- Programs being executed in the interpretive mode can be
- interrupted and changed at will in an interactive mode, ea.
- debugged "on line". Programs that are compiled and then
- executed have to go through an editing (of the program),
- compilation and execution sequence and cannot be altered during
- the testing session. The interpreter is thus used when
- developing programs for ease of debugging. The compiler can
- then be used to provide somewhat greater execution speed and to
- make the code more difficult to disassemble by others if security
- of the source code is important. Note that even when compiled,
- programs written in high level languages always run slower that
- the equivalent program written in assembly language.
-
- Computing as you will find, more and more, is a balance of
- trade-offs. The trade-off between assembly language and the high
- level language is usually that of speed (execution time) against
- ease of telling the computer what to do. In most cases the speed
- of execution of a program written in a high level language is
- still greater than the user needs. There are however instances
- when not only must assembly language be used to meet execution
- speed requirements, but the actual instructions used must
- themselves be chosen to comply with the same speed requirements,
- because different assembly language instructions have different
- (numbers of machine cycles) execution times.
-
- 1.3 Structuring Programs
-
- Good programs are well thought out, logical and structured.
- The are put together in a modular manner and structured so that
- they have a beginning, a middle and an ending. This means that
- much thought has to be applied before the program is even
- written. BASIC is often thought of as an unstructured language.
- That is not true. BASIC is usually met for the first time in its
- interpretive incarnation. That means that it is easy to sit down
- at the computer, type in a few lines of a routine and have the
- computer do something. A few more lines are then added to do
- something else. Later some more instructions are added, linking
- the first two sets. More and more instructions are added and
- linked together until a program is finished. This finished
- program is usually very unstructured because it contains numerous
- instructions to branch out of the sequence to different lines
- elsewhere in the listing. There will be forward branches to
- lines still to come, and backward branches to lines that have
- already appeared.
-
- As a result of seeing numerous programs of this kind, the
- word has gotten out that BASIC does not allow well structured
- programs to be written. Balderdash! Programs written in BASIC
- can be just as structured as those written in any other language
- if you take care to do so. That means don't just sit down at the
- machine and start typing. Sit down with your word processor,
- first define what the program is to do, then chart out the flow
- of the program, lastly sit down and enter it into the computer.
- You then get small sections of it working and link them to other
- sections as you add them. However, since you have flow charted
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 13 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- the program beforehand, you enter the various sections into the
- computer in a logical manner and a structured program results.
-
- 1.4 Flow Charts
-
- A flow chart is a tool for keeping track of the flow of a
- program. It shows in outline form what is being done and how
- decisions affect the sequence of instructions.
-
- The operations performed by a computer program fall into
- distinct categories. There are input/output operations (i/o),
- processing modules, decisions, etc. Each can be represented by
- a different shape on the flow chart as shown in figure 1.3. Flow
- charts are graphic tools, since the computer is a digital device
- most suited for word processing and other non graphical
- applications here is a way of drawing flow charts using a word
- processor. It is also designed to optimize top down program
- design and bottom up implementation.
-
- Figure 1.4a shows a typical extract from a flow chart. At
- the top there is a test or choice. Branching out from there are
- two blocks that contains tasks or procedures that are self
- contained sequences of some kind. If the condition being tested
- by the test block of the flowchart is met, the program flows
- along the YES branch and procedure B is performed. Should the
- test fail, procedure A is carried out. Both branches THEN come
- together and continue the program flow. This flowchart can be
- represented as follows;
-
- CHOICE =?
- YES DO.PROCEDURE."B"
- NO DO.PROCEDURE."A"
- THEN
-
- Note that it is also descriptive of what is being done. Figure
- 1.4b shows the same flow chart but the NO side is missing. In
- this example, there is no operation performed if the test fails.
- This is written as
-
-
- CHOICE =?
- YES DO.PROCEDURE."B"
- THEN
-
- If the reverse is true, namely nothing is done if the test
- passes, but if it fails, a procedure is carried out as shown in
- figure 1.4c, it can be written as
-
- CHOICE =?
- NO DO.PROCEDURE."A"
- THEN
-
- the indentation makes the written chart easier to read. A more
- complex extract of a flow chart is shown in figure 1.5. Here
- there are four procedures (A,B,C and D) and two choices (1 and
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 14 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- 2). This flow chart can be written as follows
-
- CHOICE.1 =?
- YES (1) DO.PROCEDURE."B"
- CHOICE.2 =?
- YES (2) DO.PROCEDURE."D"
- NO (2) DO.PROCEDURE."C"
- THEN (2)
- NO (1) DO.PROCEDURE."A"
- THEN (1).
-
- Another section of a flow chart is shown in figure 1.6. This is
- an example of a loop in which something is done (Procedure "A")
- as long as the value of the variable I has not counted down to
- zero. This can be written as
-
- SET.VALUE.OF."I"
- LOOP (on I)
- + DO.PROCEDURE."A"
- + DO.PROCEDURE."B"
- + DO.PROCEDURE."C"
- LOOPEND.
-
- The "+" is used on the first line of the chart statements
- referencing operations or procedures within a loop to signify
- that a loop is in progress. There may or may not be the need for
- the programmer to test the value of the loop counter (I),
- depending on how the loop is implemented. These conventions
- should cover all aspects of structured program design. If your
- program cannot be defined in these terms, it probably is not
- structured correctly. Figure 1.7 shows a typical flowchart
- commonly met when dissecting programs written in BASIC. As you
- can see it contains two joined loops that cannot easily be
- expressed using the convention described above. It is an
- unstructured example of the unrestricted use of the GOTO
- statement available in the BASIC language. This type of program
- is usually a result of programming at the keyboard without prior
- thought.
-
- When using this convention for writing flowcharts, it is
- desirable to have some method of indicating the start and finish
- of a procedure. Let us use the ":" (colon) to signify the
- beginning of a procedure, and the ";" (semi-colon) to signify the
- end of the procedure. This convention will make learning other
- computer languages easier later on in your programming career.
-
- It is also advisable to split the flow chart for a program
- into a number of smaller flow charts each explaining a different
- section of the program. Consider the flow charts shown in figure
- 1.8 for the operation of reading a book. The whole program can
- be explained by figure 1.8a as
-
- : READING.A.BOOK
- READ.THE.BOOK ;
-
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 15 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- Obviously, this means very little so we now have to explain how
- the procedure is carried out. This is shown in figure 1.8b as
- follows
-
- : READ.THE.BOOK
- GET.THE.BOOK
- OPEN.THE.BOOK.AT.THE.DESIRED.PAGE
- READ.THE.PAGES
- PUT.IT.AWAY.AGAIN ;
-
- not bad, now let's take a look at how the pages are read as shown
- in figure 1.8c
-
- : READ.THE.PAGES
- LOOP (Till all pages are read)
- + READ.SOMETHING.FROM.THE.PAGE
- + WHOLE.PAGE.READ =?
- + YES (1) END.OF.DESIRED.SECTION.OF.BOOK =?
- + YES (2) SET.END.OF.LOOP.FLAG
- + NO (2) TIME.TO.TURN.PAGE =?
- + YES (3) TURN.THE.PAGE
- + THEN (3)
- + THEN (2) START.AT.THE.TOP.OF.THE.NEXT.PAGE
- + THEN (1)
- LOOPEND ;
-
- Each step is described in words. The "." (period) between
- words in the description of the procedure signify that a single
- phrase is being used in the description. If spaces were used
- confusion could arise from time to time. Comments can be enclosed
- in paretheses "(" and ")". When such a chart is converted to a
- program, the labels or line numbers in the program associated
- with the statements in the flowchart can be added to the
- flowchart to make cross referencing easy.
-
- There has been no discussion as to exactly how the loop is
- implemented. That will depend on what the loop is doing.
- Different methods of implementing loops will be discussed later
- during the course of the book.
-
- Programming
-
- Programming a computer is done in three stages. In the
- first stage the action of the program is defined. The
- specification is written, the flow charts are drawn and the
- structure of the program is laid out. The flow charts are then
- converted to programming language statements in the second stage.
- Once the program is written the third stage involving the test
- and debugging occurs.
-
- Good programmers spend a lot of time performing stage 1.
- They define everything that has to be done by the program, and
- how it is to do the job. The coding process does not take too
- long. The actual time that it takes is usually a function of
- your typing speed. The test and debugging phase should not take
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-
- Chapter1 page 16 STARTREK THE COMPUTER PROGRAM By Joe Kasser
-
-
- too long if the program is well defined and the whole task to be
- performed is well understood. Programmers that spend hours
- working on their own code at the terminal correcting one bug
- after an other are not "hot shot" programmers, but rather are the
- reverse.
-
- 1.5 Bugs
-
- Errors in programs are called bugs. It is inevitable that
- errors will occur during the process of specifying, flowcharting
- and writing the program. These bugs fall mostly into three
- categories as follows.
-
- 1.5.1 Design Errors
-
- Design errors are errors made when turning the
- specifications for the program into the flowchart. This is an
- example of the computer doing what you tell it to do not what you
- wanted it to do, because a mistake was made even before the
- program was written.
-
- 1.5.2 Implementation Errors
-
- Implementation errors are made when the flow charts are
- converted to program statements. These errors are also examples
- of the computer doing what it is told to do. Implementation
- errors also include forgetting to tell the computer something,
- such as not defining a memory location in an assembly language
- program, or incorrectly writing a PASCAL procedure.
-
- 1.5.3 Typographical Errors
-
- Typographical errors are made when entering the program into
- the computer. They are usually the easiest to find because the
- computer can detect most of them. For example, if you are
- programming in BASIC and you get
-
- SYNTAX ERROR IN LINE 200
-
- the BASIC interpreter telling you that you have made some sort of
- a typographical error in line 200. If however, you type the
- wrong symbol for a variable, such as A0 instead of A1, what you
- have done is typed in A0 when you meant to enter A1. This is
- perfectly valid as far as the computer is concerned. It didn't
- know what you wanted, it only knows what you tell it. This kind
- of typographical error falls into the category of implementation
- errors.
-
-
-
-
-
-
-
-
-
-
- Copyright (c) Joe Kasser 1989
-
-
-
-
-